import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.*;

public class Main {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/lin-cms";
        String username = "root";
        String password = "123456";

        String author = "baomidou";
        String outputDir = "D://lin-cms-generator-file//";

        List<String> tables = new ArrayList<>();
        tables.add("t_student");
        tables.add("book");


        FastAutoGenerator baomidou = FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
//                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }));

        baomidou.packageConfig(builder -> {
                    builder.parent("io.github.talelin") // 设置父包名
                            .moduleName("latticy") // 设置父包模块名
                            .entity("model")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D://lin-cms-generator-file//xml//")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addTablePrefix("t_", "c_"); // 设置过滤表前缀
                    for (String s : tables) {
                        builder.addInclude(s);// 设置需要生成的表名
                    }
                    builder.entityBuilder()
                            .disable()
                            .javaTemplate("/templates/entity.java")
//                            .superClass("BaseModel")
//                            .formatFileName("%sDO")
                            .enableFileOverride()
//                            .enableLombok()
                            .addSuperEntityColumns("id", "create_time", "update_time", "delete_time", "is_deleted")
//                            .addIgnoreColumns("id", "create_time", "update_time", "delete_time", "is_deleted")

                            .controllerBuilder()
                            .template("/templates/controller.java")
//                            .superClass("BaseController")
                            .enableFileOverride()
//                            .enableHyphenStyle()
//                            .enableRestStyle()

                            .serviceBuilder()
                            .enableFileOverride()
                            .serviceTemplate("/templates/service.java")
                            .serviceImplTemplate("/templates/serviceImpl.java")
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
//                            .superServiceClass("BaseService")
//                            .superServiceImplClass("BaseServiceImpl")

                            .mapperBuilder()
                            .enableFileOverride()
                            .mapperTemplate("/templates/mapper.java")
//                            .superClass("BaseMapper")
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                    ;
                })
                .injectionConfig(consumer -> {

                    List<CustomFile> customFiles = new ArrayList<>();
                    // DTO
//                    customFiles.add(new CustomFile.Builder().packageName("dto").fileName("DTO.java")
//                            .templatePath("/templates/entityDTO.java.ftl").enableFileOverride().build());
//                    customFiles.add(new CustomFile.Builder().packageName("dto").fileName("ValidatedDTO.java")
//                            .templatePath("/templates/entityValidatedDTO.java.ftl").enableFileOverride().build());

                    // 前端
                    String frontPath = "D://lin-cms-generator-file//front//";
                    customFiles.add(new CustomFile.Builder().packageName("stage").fileName("首字母改小写.js")
                            .templatePath("/vue/config.js.ftl").enableFileOverride().filePath(frontPath + "src//config//").build());

                    customFiles.add(new CustomFile.Builder().packageName("放到对应entity文件夹").formatNameFunction(tableInfo -> tableInfo.getEntityName().toLowerCase()).fileName("-list.vue")
                            .templatePath("/vue/List.vue.ftl").enableFileOverride().filePath(frontPath + "src//views//").build());
                    customFiles.add(new CustomFile.Builder().packageName("放到对应entity文件夹").formatNameFunction(tableInfo -> tableInfo.getEntityName().toLowerCase()).fileName(".vue")
                            .templatePath("/vue/update.vue.ftl").enableFileOverride().filePath(frontPath + "src//views//").build());

                    customFiles.add(new CustomFile.Builder().packageName("models").formatNameFunction(tableInfo -> tableInfo.getEntityName().toLowerCase()).fileName(".js")
                            .templatePath("/vue/entity.js.ftl").enableFileOverride().filePath(frontPath + "src//").build());

                    consumer.customFile(customFiles);
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();



        FastAutoGenerator baomidou2 = FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
//                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }));

        baomidou2.packageConfig(builder -> {
                    builder.parent("io.github.talelin") // 设置父包名
                            .moduleName("latticy")
                            .entity("dto")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D://lin-cms-generator-file//xml//"));;
                })
                .strategyConfig(builder -> {
                    builder.addTablePrefix("t_", "c_"); // 设置过滤表前缀
                    for (String s : tables) {
                        builder.addInclude(s);// 设置需要生成的表名
                    }
                    builder.entityBuilder()
                            .javaTemplate("/templates/entityValidatedDTO.java")
                            .formatFileName("CreateOrUpdate%sDTO")
                            .enableFileOverride()
                            .addSuperEntityColumns("id", "create_time", "update_time", "delete_time", "is_deleted")

                            .serviceBuilder().disable()
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();


        FastAutoGenerator baomidou3 = FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
//                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }));

        baomidou3.packageConfig(builder -> {
                    builder.parent("io.github.talelin") // 设置父包名
                            .moduleName("latticy")
                            .entity("model")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D://lin-cms-generator-file//xml//"));;
                })
                .strategyConfig(builder -> {
                    builder.addTablePrefix("t_", "c_"); // 设置过滤表前缀
                    for (String s : tables) {
                        builder.addInclude(s);// 设置需要生成的表名
                    }
                    builder.entityBuilder()
                            .javaTemplate("/templates/entity.java")
                            .formatFileName("%sDO")
                            .enableFileOverride()
                            .addSuperEntityColumns("id", "create_time", "update_time", "delete_time", "is_deleted")

                            .serviceBuilder().disable()
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
